package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.ReimbursementDao;
import com.bdqn.oa.entity.Reimbursement;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Chenrz
 * @date 2022/7/20 17:05
 */
public class ReimbursementDaoImpl implements ReimbursementDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增报销
     *
     * @param rei
     * @return
     * @throws SQLException
     */
    @Override
    public int saveRei(Reimbursement rei) throws SQLException {
        String sql = "insert into `reimbursement`(employee_id,tile,content,datetime,money,`status`) " +
                " values (?,?,?,?,?,?);";
        Object[] params = {
                rei.getEmployeeId(),rei.getTile(),rei.getContent(),
                rei.getDatetime(),rei.getMoney(),rei.getStatus()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 通过id删除报销
     *
     * @param employeeId
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteRei(Integer employeeId) throws SQLException {
        String sql = "delete from `reimbursement` where employee_id = ?;";
        return queryRunner.update(DruidUtils.getConnection(),sql,employeeId);
    }

    /**
     * 修改报销
     *
     * @param rei
     * @return
     * @throws SQLException
     */
    @Override
    public int updateRei(Reimbursement rei) throws SQLException {
        String sql = "update `reimbursement` set `tile`=?,`content`=?,`datetime`=?,`money`=?,`status`=? where employee_id=?;";
        Object[] params = {
                rei.getTile(),rei.getContent(),rei.getDatetime(),
                rei.getMoney(),rei.getStatus(),rei.getEmployeeId()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 审批报销
     *
     * @param rei
     * @return
     * @throws SQLException
     */
    @Override
    public int updateReiByStatus(Reimbursement rei) throws SQLException {
        String sql = "update `reimbursement` set `status`=?,`comment`=?,`comment_id`=?,`comment_datetime`=? where employee_id = ?;";
        Object[] params = {
                rei.getStatus(),rei.getComment(),rei.getCommentId(),rei.getCommentDatetime(),rei.getEmployeeId()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 通过员工id查询报销
     *
     * @param employeeId
     * @return
     * @throws SQLException
     */
    @Override
    public List<Reimbursement> getListReiById(Integer employeeId) throws SQLException {
        List<Reimbursement> ReiList = new ArrayList<Reimbursement>();
        String sql = "select `id`,`employee_id`,`type`,`content`,`datetime`,`start_time`,`end_time`,`status`," +
                "`comment`,`comment_name`,`comment_datetime` from `leave` where `employee_id` = ?;";
        ReiList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Reimbursement>(Reimbursement.class),employeeId);
        return ReiList;
    }

    /**
     * 查询某一天的所有报销
     *
     * @param datetime
     * @return
     * @throws SQLException
     */
    @Override
    public List<Reimbursement> getListReiByDay(Date datetime) throws SQLException {
        List<Reimbursement> ReiList = new ArrayList<Reimbursement>();
        String sql = "select `id`,`employee_id`,`tile`,`content`,`datetime`,`money`," +
                "`status`,`comment`,`comment_id`,`comment_datetime` from `reimbursement` where datetime=?;";
        ReiList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Reimbursement>(Reimbursement.class),datetime);
        return ReiList;
    }

    /**
     * 查询所有报销
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Reimbursement> getAllListRei() throws SQLException {
        List<Reimbursement> ReiList = new ArrayList<Reimbursement>();
        String sql = "select `id`,`employee_id`,`tile`,`content`,`datetime`,`money`," +
                "`status`,`comment`,`comment_id`,`comment_datetime` from `reimbursement`;";
        ReiList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Reimbursement>(Reimbursement.class));
        return ReiList;
    }
}
